package cn.sinjet.myview;

import com.mgroup.wazehelper.R;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.widget.FrameLayout;
import cn.sinjet.viewmodel.ViewEvent;
import cn.sinjet.viewmodel.ViewModel;
/*
 * The View is for HUD setting simulator with gesture detector 
 * */
public class GestureSetView extends FrameLayout{
	final String tag = GestureSetView.class.getSimpleName();
    private GestureDetector mGestureDetector;
	public GestureSetView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	public GestureSetView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		LayoutInflater.from(context).inflate(R.layout.gesture_set_view, this,true);
		mGestureDetector = new GestureDetector(context,new MyGestureListener());
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		//super.onTouchEvent(event);
		return mGestureDetector.onTouchEvent(event);
	}
	class MyGestureListener implements GestureDetector.OnGestureListener{

		@Override
		public boolean onDown(MotionEvent arg0) {
			// TODO Auto-generated method stub
			return true;
		}

		@Override
		public boolean onFling(MotionEvent e0, MotionEvent e1, float offsetX, float offsetY) {
			// TODO Auto-generated method stub
//			Log.d(tag, "onFling e0:"+e0.getX()+","+e0.getY()+" e1:"+e1.getX()+","+e1.getY()+" x:"+offsetX+" y:"+offsetY);
			if(offsetY >10) 
				onFingDown();
			else if(offsetY < -10)
				onFingUp();
			return true;
		}

		@Override
		public void onLongPress(MotionEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2, float arg3) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public void onShowPress(MotionEvent arg0) {
			// TODO Auto-generated method stub
		    Log.d(tag, "onShowPress");	
		}

		@Override
		public boolean onSingleTapUp(MotionEvent arg0) {
			// TODO Auto-generated method stub
//			Log.d(tag, "onSingleTapUp");
			onPressOK();
			return true;
		}
	
	}
	
	private void onFingUp(){//up
		Log.d(tag, "onFingUp");
		ViewModel.getIns().onViewEvent(new ViewEvent(R.id.gesture_up, ViewEvent.CLICK));
	}
	private void onFingDown(){//down
		Log.d(tag, "onFingDown");
		ViewModel.getIns().onViewEvent(new ViewEvent(R.id.gesture_down, ViewEvent.CLICK));
	}
	private void onPressOK(){//ok
		Log.d(tag, "onPressOK");
		ViewModel.getIns().onViewEvent(new ViewEvent(R.id.gesture_ok, ViewEvent.CLICK));
	}
	
}
